//
//  LJYVideoViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/28.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYVideoViewController.h"
#import "LJYVideoCollectionReusableView.h"
#import "LJYVideoCollectionViewCell.h"
#import "LJYPlayVideoViewController.h"
#import "LJYVideoTypeViewController.h"
#import "LJYVideoModel.h"
#import "AppTools.h"
#import "MJRefresh.h"

#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

@interface LJYVideoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, LJYVideoCollectionReusableViewDelegate>

@property(nonatomic, strong)NSMutableArray *VideoArray;
@property(nonatomic, strong)NSMutableArray *topButtonArray;
@property (nonatomic, assign) NSInteger page;
@property(nonatomic, copy)NSString *urlStr;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic, strong)LJYVideoCollectionReusableView *reuseableView;

@end

@implementation LJYVideoViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrame" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrameBack" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    

    self.page = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    _topButtonArray = [NSMutableArray array];
    _VideoArray = [NSMutableArray array];
    [self creatCollectionView];
    
}
-(void)creatCollectionView
{
     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.estimatedItemSize = CGSizeMake(SCREEN_WIDTH-30, 300);
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 100);
    flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 100);

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[LJYVideoCollectionViewCell class] forCellWithReuseIdentifier:@"LJYVideoCollectionViewCell"];
    [self.collectionView registerClass:[LJYVideoCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionReusableViewfoot"];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downTableView)];

}

- (void)upTableView
{
    self.urlStr = @"http://c.3g.163.com/nc/video/home/0-10.html";
    self.page = 0;
    [self addData: self.urlStr andDwon:NO];
}

#pragma mark - 上拉刷新
- (void)downTableView{
    
    self.page = self.page + 10;
    
    // 拼接网址
    self.urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%ld-10.html", self.page];
    
    [self addData:self.urlStr andDwon:YES];
}

- (void)addData:(NSString *)url andDwon:(BOOL)isDown
{
    [AppTools getDataWithURL:url Block:^(id result) {
        
        if (!isDown)
        {
            [self.VideoArray removeAllObjects];
        }
        NSDictionary *dictionary = result;
//        NSMutableArray *array = dictionary[@"videoSidList"];
        NSMutableArray *arrayVideo = dictionary[@"videoList"];
        
//        for (NSDictionary *dic in array)
//        {
//            LJYVideoTopButtonModel *topButton = [[LJYVideoTopButtonModel alloc] init];
//            [topButton setValuesForKeysWithDictionary:dic];
//        self.topButtonArray = [NSMutableArray arrayWithArray:array];
//        self.reuseableView.topButtonArray = self.topButtonArray;
//        }
        
        for (NSDictionary *dic in arrayVideo)
        {
            LJYVideoModel *video = [[LJYVideoModel alloc] init];
            [video setValuesForKeysWithDictionary:dic];
            [self.VideoArray addObject:video];
        }
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [self.collectionView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error = %@", error);
        
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.VideoArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJYVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LJYVideoCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    cell.video = [self.VideoArray objectAtIndex:indexPath.row];
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader)
    {
        self.reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];
        self.reuseableView.delegate = self;
        [AppTools getDataWithURL:@"http://c.3g.163.com/nc/video/home/0-10.html" Block:^(id result) {
            
            NSDictionary *dictionary = result;
            NSMutableArray *array = dictionary[@"videoSidList"];
            self.topButtonArray = [NSMutableArray arrayWithArray:array];
            self.reuseableView.topButtonArray = self.topButtonArray;
        } ErrorBlock:^(NSError *error) {
            
            
        }];
        
//        [self.collectionView reloadData];
//        self.reuseableView.topButtonArray = self.topButtonArray;
//        NSLog(@"topButtonArray = %@", self.topButtonArray);
        self.reuseableView.backgroundColor = [UIColor whiteColor];
        return self.reuseableView;
    }
    else
    {
        UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableViewfoot" forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor clearColor];
        return reuseableView;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJYVideoModel *model = [self.VideoArray objectAtIndex:indexPath.row];
    LJYPlayVideoViewController *videoPlay = [[LJYPlayVideoViewController alloc] init];
    videoPlay.url = model.mp4_url;
//    [self.navigationController pushViewController:videoPlay animated:YES];
    [self presentViewController:videoPlay animated:YES completion:^{
        
        
    }];

}
-(void)gotoOtherTypeVideoMethodWithModel:(LJYVideoTopButtonModel *)model
{
    LJYVideoTypeViewController *typeView = [[LJYVideoTypeViewController alloc] init];
    typeView.topButton = model;
    [self.navigationController pushViewController:typeView animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrameVidio" object:nil];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrameVidioBack" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playPause" object:nil];
    

}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
